#include "Protocol.h"
#include<avr/io.h>
//default constructor
template<typename Implementation>
Protocol<Implementation>::Protocol()
{
}
//constructor sets the implementation
template<typename Implementation>
Protocol<Implementation>::Protocol(Implementation* i)
{
	implementation = i;
}

//configures the protocol
template<typename Implementation>
void Protocol<Implementation>::config()
{
	implementation->config();
}
//tells if an error occured in transmission
template<typename Implementation>
bool Protocol<Implementation>::error()
{
	return implementation->getError();
}

//tells if parity is correct
template<typename Implementation>
bool Protocol<Implementation>::parity()
{
	return implementation->getParity();
}

//runs the protocol doing a polling format.
template<typename Implementation>
void Protocol<Implementation>::run()
{ 
	if(implementation->getReceiveFinished()==true)
	{
		//get value transmitted and add to the queue
		//if(implementation->getOut() != 0 || implementation->getOut() != 0xFF)
		PORTA = ~PORTA;
			setOutput(implementation->getOut());
	}
	if(implementation->getTransmitFinished()==true)
	{		
		//set the next chunk of data to transmit.
		if(inputEmpty()==false)
		{
			implementation->setIn(getInput());
		}
	}
	implementation->run();
}
//transmits data
template<typename Implementation>
void Protocol<Implementation>::transmitData()
{
	implementation->transmitData();
}

//receives new data
template<typename Implementation>
void Protocol<Implementation>::receiveData()
{
	implementation->receiveData();
}
template<typename Implementation>
void Protocol<Implementation>::scheduleTransmitInterrupt()
{
	implementation->scheduleTransmitInterrupt();
}

template<typename Implementation>
void Protocol<Implementation>::scheduleReceiveInterrupt()
{
	implementation->scheduleReceiveInterrupt();
}

template<typename Implementation>
bool Protocol<Implementation>::transmitFinished()
{
	return implementation->getTransmitFinished();
}

template<typename Implementation>
bool Protocol<Implementation>::receiveFinished() 
{
	return implementation->getReceiveFinished();
}

template<typename Implementation>
void Protocol<Implementation>::init()
{
	implementation->init();
}

template<typename Implementation>
void Protocol<Implementation>::resetReceive()
{
	implementation->resetReceive();
}

template<typename Implementation>
void Protocol<Implementation>::resetTransmit()
{
	implementation->resetTransmit();
}

